<?php
class Zendvn_System_Info{
	
	private $_ss;
	
	public function __construct(){		
		$this->_ss = new Zend_Session_Namespace(APPLICATION_KEY);
		$this->_ss->setExpirationSeconds(999999);
	}
	
	public function clearInfo(){
		$this->_ss->unsetAll();
	}
	
	public function setInfo($auth = null, $type = null){	
		if($auth != null){
			if($type == null){
				$this->setType();	
				$this->setLanguage();			
				$this->setMemberInfo($auth->getIdentity()->id);
				$this->setGroupInfo($auth->getIdentity()->id);
			}elseif($type == 'cookie'){
				$this->setType();	
				$this->setLanguage();			
				$this->setMemberInfoEmail($auth['user_email']);
				$this->setGroupInfoEmail($auth['user_email']);
			}
		}
	}
	
	public function getInfo($part = null){
		$info = $this->_ss->getIterator();
		if($part != null){
			$info = $info[$part];
		}
		return $info;
	}
	
	public function setType($value = 'Member'){
		$this->_ss->type = $value;
	}
	
	public function getType(){
		return $this->_ss->type;
	}
	
	public function setMemberInfoEmail($member_email = null){
		if($member_email != null){
			$db = Zend_Db_Table::getDefaultAdapter();
			$select = $db->select()->from('users')->where('email = ?', $member_email, STRING);	
			$result = $db->fetchRow($select);
			$this->_ss->member = $result;
		}
	}
	
	public function setMemberInfo($member_id = null){
		if($member_id != null){
			$db = Zend_Db_Table::getDefaultAdapter();
			$select = $db->select()->from('users')->where('id = ?',$member_id,1);	
			$result = $db->fetchRow($select);
			$this->_ss->member = $result;
		}
	}
	
	public function getMemberInfo($part = null){
		if($part == null){
			$info = $this->_ss->member;
		}else{
			$info = $this->_ss->member;
			$info = $info[$part];
		}
		return $info;
		
	}
	
	public function setGroupInfoEmail($member_email = null){
		if($member_email != null){
			$db = Zend_Db_Table::getDefaultAdapter();
			$select = $db->select()
						 ->from('user_group AS g')
						 ->join('user_and_group AS ug', 'ug.user_group_id = g.id',array('group_default'))
						 ->joinLeft('users AS u', 'ug.user_id = u.id', null)
						 ->where('u.email = ?', $member_email, STRING);	
			$result = $db->fetchAll($select);			
			$this->_ss->group = $result;
		}
	}
	
	public function setGroupInfo($member_id = null){
		if($member_id != null){
			$db = Zend_Db_Table::getDefaultAdapter();
			$select = $db->select()
						 ->from('user_group AS g')
						 ->join('user_and_group AS ug', 'ug.user_group_id = g.id',array('group_default'))
						 ->where('ug.user_id	 = ?',$member_id,1);	
			$result = $db->fetchAll($select);			
			$this->_ss->group = $result;
		}
	}
	
	public function getGroupInfo($part = null){
		if($part == null){
			$info = $this->_ss->group;
		}else{
			$info = $this->_ss->group;
			$info = $info[$part];
		}
		return $info;
	}
	
	public function getLanguage($area = 'public',$part = 'interface'){
		$info = $this->_ss->language;
		$info = $info[$area][$part];
		return $info;
	}
	
	
	public function setLanguage($options = null,$arrParam = null,$area = 'public'){
		if($options ==  null){
			$lang = new Zendvn_Languages();
			$publicLang = array('interface'=>$lang->getLangDefault(),
						  		'database'=>$lang->getLangDefault());
			$adminLang = array('interface'=>$lang->getLangDefault(),
						  		'database'=>$lang->getLangDefault());
			$this->_ss->language = array('admin' => $adminLang,'public' =>$publicLang);
		}else{
			$info = $this->_ss->language;
			$info[$area][$arrParam['type']] = $arrParam['lang'];
			$this->_ss->language = $info;
			echo '<pre>';
			print_r($info);
			echo '</pre>';
		}
		
	}
}

